#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""DocString

DocString Paragraph

"""
import imp
import sys

import logging
import logging.config
logger = logging.getLogger('bannot')


module_types = {    imp.PY_SOURCE: 'source',
                    imp.PY_COMPILED: 'compiled',
                    imp.C_EXTENSION: 'extension',
                    imp.PY_RESOURCE: 'resource',
                    imp.PKG_DIRECTORY: 'package'
                }


class Module_loader:
    """ Clase encargada de cargar los diferentes módulos indicados en el archivo
        de definidos en el modules.cfg"""
    def __init__(self, pkgs):
        """ La variable path define dónde va está el archivo de definición de
            modulos
        """
        self.pkgs = pkgs
        for pkg in sorted(pkgs.keys()): 
            try:
                f, pkg_fname, description = imp.find_module(pkg)
                try:
                    ex_pkg = imp.load_module(pkg, f, pkg_fname, description)
                    logger.info('Pkg load: %s; Path: %s', pkg, pkg_fname)
                finally:
                    if f:
                        f.close()
                for mod in pkgs[pkg]:
                    try:
                        f, mod_fname, description = imp.find_module(mod, [pkg_fname])
                        try:
                            ex_mod = imp.load_module(pkg + '.' + mod, f, mod_fname, description)
                            logger.info('Mod load: %s; Path: %s', mod, mod_fname)
                        finally:
                            if f:
                                f.close()
                    except ImportError as err:
                        logger.error('Mod not found: %s; Err: %s', mod, err)
            except ImportError as err:
                logger.error('Pkg not found: %s; Err: %s', pkg, err)


    def __str__(self):
        return str(sys.modules)

    def get_path(self):
        return self.pkgs

    def set_path(self, pkgs):
        self.path = pkgs

__version__ = "$Revision$"
# $Source$

